// 这份代码专门处理逻辑层的操作
const checkinDao = require('../dao/checkinDao');

const Response = require('../entity/http/Response');

const checkinService = {
    /*
     * 专注模式打卡
     */
    async focusCheckIn(uid, checkin) {
        let { time, duration, now_time } = checkin;
        let result;
        try {
            let res = await checkinDao.focusCheckIn(uid, time, duration, now_time);
            if (res.insertId) {
                result = (new Response()).ok("专注模式打卡成功", {});
            } else {
                result = (new Response()).fail(500, "专注模式打卡失败");
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，打卡失败");
        }
        return result;
    },
    // 获取今日专注时长
    async getTodayfocusDuration(uid) {
        let result;
        try {
            let res = await checkinDao.getTodayfocusDuration(uid);
            if (res.length) {
                result = (new Response()).ok("操作成功", { todayMinutes: res[0].total_duration || 0 });
            } else {
                result = (new Response()).ok("操作成功", { todayMinutes: 0 });
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，获取失败");
        }
        return result;
    },
    // /*
    //  * 打卡统计
    //  */
    // async checkinStatistics(uid) {
    //     let result;
    //     try {
    //         let res = await checkinDao.checkinStatistics(uid);
    //         result = { success: true, msg: "打卡数据统计成功", data: res[0] };
    //     } catch (error) {
    //         result = { success: false, msg: "操作数据库时出错" };
    //     }
    //     return result;
    // },
    // /*
    //  * 排行榜接口
    //  */
    // async checkinRanking(uid) {
    //     let result;
    //     try {
    //         let res = await checkinDao.checkinRanking(uid);
    //         result = { success: true, msg: "排行榜统计成功", data: res };
    //     } catch (error) {
    //         result = { success: false, msg: "操作数据库时出错" };
    //     }
    //     return result;
    // },
    // /*
    //  * 最近7天的打卡趋势
    //  */
    // async checkinTrend(uid) {
    //     let result;
    //     try {
    //         let res = await checkinDao.checkinTrend(uid);
    //         result = { success: true, msg: "统计成功", data: res };
    //     } catch (error) {
    //         result = { success: false, msg: "操作数据库时出错" };
    //     }
    //     return result;
    // },
    // async getMainPageNeedInfo(uid) {
    //     let result;
    //     try {
    //         let res = await checkinDao.getMainPageNeedInfo(uid);
    //         if (res.length) {
    //             result = { success: true, msg: "统计成功", data: res[0] };
    //         } else {
    //             result = { success: false, msg: "统计失败" };
    //         }
    //     } catch (error) {
    //         result = { success: false, msg: "操作数据库时出错" };
    //     }
    //     return result;
    // }
    async dailyCheckin(uid) {
        let result;
        try {
            let res = await checkinDao.dailyCheckin(uid);
            if (res.insertId) {
                result = (new Response()).ok("操作成功", {});
            } else {
                result = (new Response()).ok("今日已打卡", {});
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，打卡失败");
        }
        return result;
    },
    async getDailyCheckinContinuousDays(uid) {
        let result;
        try {
            let res = await checkinDao.getDailyCheckinContinuousDays(uid);
            if (res.length) {
                result = (new Response()).ok("操作成功", { continuousDays: res[0].continuousDays });
            } else {
                result = (new Response()).ok("操作成功", { continuousDays: 0 });
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，获取失败");
        }
        return result;
    },
    async addTodo(uid, todo) {
        let result;
        try {
            result = await checkinDao.addTodo(uid, todo);
            if (result.insertId) {
                result = (new Response()).ok("操作成功", result.insertId);
            } else {
                result = (new Response()).fail(400, "操作失败");
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，创建待办事项失败");
        }
        return result;
    },
    async getAllTodos(uid) {
        let result;
        try {
            result = await checkinDao.getAllTodos(uid);
            result = (new Response()).ok("操作成功", result);
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，获取所有待办事项失败");
        }
        return result;
    },
    async setTodoStatus(uid, todoId, status) {
        let result;
        try {
            result = await checkinDao.setTodoStatus(uid, todoId, status);
            if (result.changedRows) {
                result = (new Response()).ok("操作成功");
            } else {
                result = (new Response()).fail(500, "操作失败");
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，修改待办事项状态失败");
        }
        return result;
    },
    async deleteTodo(uid, todoId) {
        let result;
        try {
            result = await checkinDao.deleteTodo(uid, todoId);
            if (result.affectedRows) {
                result = (new Response()).ok("操作成功");
            } else {
                result = (new Response()).fail(500, "这个待办是你的吗你就删");
            }
        } catch (error) {
            result = (new Response()).fail(500, "操作数据库时出错，完成待办事项失败");
        }
        return result;
    }
};

module.exports = checkinService;
